<?php
declare (strict_types = 1);

namespace app\controller;

use app\BaseController;
use think\facade\Db;
use think\facade\View;
use think\Request;

class Tools extends Common
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {
        $t = input("param.t","index");
        View::assign("title","程序员在线工具");
        return  view($t);
    }

    //时间戳转换成北京时间
    public function timestamp()
    {
        $t = input("post.t");
        $data = date("Y-m-d H:i:s",(int)($t));
        return $data;
    }
    //北京时间到时间戳
    public function beijing()
    {
        $t = input("post.t");
        $data = strtotime($t);
        return $data;
    }

    function en_url(){
        $t = input("post.t");
        $data = urlencode($t);
        return $data;
    }

    function de_url(){
        $t = input("post.t");
        $data = urldecode($t);
        return $data;
    }

    function md(){
        $t = input("post.t");
        $data = md5($t);
        return $data;
    }
    function clear_html(){
        $t = input("post.t");
        $data = strip_tags($t);
        return $data;
    }

    public function test()
    {
        htmlspecialchars_decode();
        htmlspecialchars();
        strip_tags();
    }

    //将浏览器的headers转为数组或者json
    public function headers()
    {
        if(\request()->isAjax()){
            $type = \request()->param("type","","trim");
            $t = \request()->param("t","","trim");
            if($type == 'array'){
                $arr = explode(PHP_EOL,$t);
                $temp = '['.PHP_EOL;
                foreach ($arr as $k => $v){
                    $d = explode(": ",$v);
                    //$temp[$d[0]] = $d[1];
                    if($k >= (count($arr)-1) ){
                        $temp .= '    "'.$d[0].'" =>'.'"'.$d[1].'"'.PHP_EOL;
                    }else{
                        $temp .= '    "'.$d[0].'" =>'.'"'.$d[1].'",'.PHP_EOL;
                    }
                }
                $temp .= ']';
                return (string)$temp;
            }

            if($type == 'json'){
                $arr = explode(PHP_EOL,$t);
                $temp = '{'.PHP_EOL;
                foreach ($arr as $k => $v){
                    $d = explode(": ",$v);
                    //$temp[$d[0]] = $d[1];
                    if($k >= (count($arr)-1) ){
                        $temp .= '    "'.$d[0].'" :'.'"'.$d[1].'"'.PHP_EOL;
                    }else{
                        $temp .= '    "'.$d[0].'" :'.'"'.$d[1].'",'.PHP_EOL;
                    }
                }
                $temp .= '}';
                return (string)$temp;
            }
        }
        return  view();
    }
}
